<?php
class AlbumPhoto extends ARAlbumPhoto {
	public $maxInsert = 18;
	public $rotate = 0;
	private $_thumbSize = array(
		'view' => array(
			'width' => 940,
			'height' => 0 
		),
		'thumb' => array(
			'width' => 220,
			'height' => 160 
		),
		'preview' => array(
			'width' => 186,
			'height' => 139 
		) 
	);

	public function rules () {
		static $rules;
		
		return empty($rules) ? $rules = array(
			array(
				'aid, path',
				'required',
				'on' => 'insert' 
			),
			array(
				'name',
				'filter',
				'filter' => 'htmlspecialchars' 
			),
			array(
				'description',
				'length',
				'min' => 4,
				'max' => 65535 
			),
			array(
				'aid, authorId',
				'numerical' 
			),
			array(
				'rotate',
				'rotate' 
			),
			array(
				'path',
				'copyImage',
				'on' => 'insert' 
			),
			array(
				'lastUpdateTime',
				'default',
				'value' => BEGIN_TIME,
				'setOnEmpty' => false,
				'on' => 'insert' 
			) 
		) : $rules;
	}

	public function copyImage ($attribute, $param) {
		$attachment = Yii::app()->attachment;
		$path = $this->$attribute;
		if (!is_file($attachment->getPath($path)))
			$this->addError($attribute, '图片不存在,不能保存');
	}

	public function rotate ($attribute, $param) {
		if ($rotate = $this->$attribute) {
			$file = Yii::app()->attachment->getPath($this->path);
			Image::create($file)->rotateImageNDegrees($rotate - ($rotate % 90))->save($file);
		}
	}

	/**
	 * 获取缩略图地址
	 * 
	 * @param string $path        	
	 * @param string $name        	
	 * @param boolean $absoluteUrl        	
	 * @param boolean $create        	
	 * @return multitype:NULL
	 */
	public function getThumb ($path, $name = null, $absoluteUrl = false, $create = false) {
		$sizeArray = $this->getThumbSize($name);
		$attachment = Yii::app()->attachment;
		if ($name)
			return $attachment->getThumb($path, $sizeArray['width'], $sizeArray['height'], $absoluteUrl, $create);
		else {
			$thumbArray = array();
			foreach($sizeArray as $name => $size) {
				$thumbArray[$name] = $attachment->getThumb($path, $size['width'], $size['height'], $absoluteUrl, $create);
			}
			return $thumbArray;
		}
	}

	/**
	 * 设置缩略图尺寸
	 * 
	 * @param sting $name        	
	 * @param array $size
	 *        	例如 array('width' => 100, 'height' => 100)
	 */
	public function setThumbSize ($name, $size) {
		if (isset($size['width']) && isset($size['height']))
			$this->_thumbSize[$name] = $size;
		throw new CException('缩略图尺寸缺少参数');
	}

	public function getThumbSize ($name = null) {
		if ($name && isset($this->_thumbSize[$name]))
			return $this->_thumbSize[$name];
		else
			return $this->_thumbSize;
	}

	/**
	 * 增加评论
	 * 
	 * @param AlbumComment $comment        	
	 */
	public function addComment (AlbumComment $comment) {
		$comment->pid = $this->pid;
		if($return = $comment->save())
			$this->updateReplies();
		return $return;
	}

	/**
	 * 更新相册评论数
	 * 
	 * @param int $num        	
	 */
	public function updateReplies ($num = 1) {
		return $this->updateByPk($this->pid, array(
			'replies' => $this->replies + (int) $num 
		));
	}
}